﻿/***********************************************************
Copyright © 2015 Howyoung.
************************************************************
You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that
you conspicuously and appropriately publish on each copy an
appropriate copyright notice and disclaimer of warranty.
************************************************************
您可以对所收受的本程序源代码，无论以何种媒介，复制与发布其完
整的复制物，然而您必须以显著及适当的方式在每一份复制物上发布
适当的著作权标示及无担保声明。
***********************************************************/
using System;
using System.Windows.Forms;

namespace MinecraftSavesBackup
{
    class HandlingErrors
    {
        public static void Record(Exception ex)
        {
            string log, osVersion;

            if (Environment.OSVersion.VersionString.Contains("5.0"))
            {
                osVersion = "Windows 2000";
            }
            else if (Environment.OSVersion.VersionString.Contains("5.1"))
            {
                osVersion = "Windows XP";
            }
            else if (Environment.OSVersion.VersionString.Contains("5.2"))
            {
                osVersion = "Windows Server 2003, Windows XP 64-bit Edition";
            }
            else if (Environment.OSVersion.VersionString.Contains("6.0"))
            {
                osVersion = "Windows Vista, Windows Server 2008";
            }
            else if (Environment.OSVersion.VersionString.Contains("6.1"))
            {
                osVersion = "Windows 7, Windows Server 2008 R2";
            }
            else if (Environment.OSVersion.VersionString.Contains("6.2"))
            {
                osVersion = "Windows 8, Windows RT, Windows Server 2012";
            }
            else if (Environment.OSVersion.VersionString.Contains("6.3"))
            {
                osVersion = "Windows 8.1, Windows RT 8.1, Windows Server 2012 R2";
            }
            else if (Environment.OSVersion.VersionString.Contains("10.0"))
            {
                osVersion = "Windows 10";
            }
            else
            {
                osVersion = "Unknown";
            }

            log = DateTime.Now.ToString() + " " + ex.StackTrace + " " + ex.Message + Environment.NewLine
                + Environment.NewLine
                + "[ERROR INFORMATION]" + Environment.NewLine
                + "Time:" + DateTime.Now.ToString() + Environment.NewLine
                + "Source:" + ex.Source + Environment.NewLine
                + "StackTrace:" + ex.StackTrace + Environment.NewLine
                + "TargetSite:" + ex.TargetSite + Environment.NewLine
                + "Message:" + ex.Message + Environment.NewLine
                + Environment.NewLine
                + "[SYSTEM INFORMATION]" + Environment.NewLine
                + "Is64BitOperatingSystem:" + Environment.Is64BitOperatingSystem + Environment.NewLine
                + "MachineName:" + Environment.MachineName + Environment.NewLine
                + "OSVersion:" + Environment.OSVersion + " (" + osVersion + ")" + Environment.NewLine
                + "ProcessorCount:" + Environment.ProcessorCount + Environment.NewLine
                + "StackTrace:" + Environment.NewLine + Environment.StackTrace + Environment.NewLine
                + "SystemPageSize:" + Environment.SystemPageSize + Environment.NewLine
                + "WorkingSet:" + Environment.WorkingSet;

            Properties.Settings.Default.ErrorLog.Add(log);
            Properties.Settings.Default.Save();
        }

        public static void ErrorMessage(Exception ex, string solution)
        {
            MessageBox.Show(":(" + Environment.NewLine + "很抱歉，我们遇到了麻烦。" + Environment.NewLine + Environment.NewLine +
                "这是可能有用的信息：" + Environment.NewLine + ex.Message + Environment.NewLine + Environment.NewLine +
                "您可以试试：" + Environment.NewLine + solution + Environment.NewLine + Environment.NewLine +
                "如果问题还未得到解决，请转到“帮助-查看错误日志”将日志提交给我们。", "出现错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}
